// misc.cpp
//
// Copyright (C) 2008  Maksim Sipos <msipos@mailc.net>
//
// This file is a part of the Ripe language project.
//
// Ripe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include "util/misc.hpp"

namespace Ripe {

uint32_t map_prime(uint32_t n)
{
  /* Size of the sizes[] list */
  #define MAP_PRIMES 27

  /* This list was generated by a script that is in closet. */
  const uint32_t sizes[] = {13, 31, 61, 127, 251, 509, 1021, 2039, 4093, 8191,
                            16381, 32749, 65521, 131071, 262139, 524287,
                            1048573, 2097143, 4194301, 8388593, 16777213,
                            33554393, 67108859, 134217689, 268435399,
                            536870909, 1073741789};

  int i;
  for (i = 0; i < MAP_PRIMES - 1; i++) {
    if (n < sizes[i])
      return sizes[i];
  }

  /* Otherwise, return the largest possible hash table size. */
  return sizes[MAP_PRIMES - 1];
}

}
